<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册账号</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        p {
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<form action="${pageContext.request.contextPath}/user" id="registerForm" method="post">
    <h2>注册账号</h2>
    <input type="hidden" name="action" value="register">
    <label for="username">用户名</label>
    <input type="text" id="username" name="username" required>
    <p id="usernameError" class="error"></p>
    <label for="email">邮箱</label>
    <input type="email" id="email" name="email" required>
    <p id="emailError" class="error"></p>
    <label for="password">密码</label>
    <input type="password" id="password" name="password" required>
    <p id="passwordError" class="error"></p>
    <button type="submit">注册</button>
    <p>已有账号？<a href="/login.jsp">立即登录</a></p>
</form>
<script>
    document.getElementById('registerForm').addEventListener('submit', function (e) {
        e.preventDefault();
        let isValid = true;
        // 验证用户名
        const username = document.getElementById('username').value;
        const usernameRegex = /^[a-zA-Z0-9_]{4,20}$/;
        if (!usernameRegex.test(username)) {
            document.getElementById('username').classList.add('error');
            document.getElementById('usernameError').textContent = '用户名格式错误！';
            isValid = false;
        } else {
            document.getElementById('username').classList.remove('error');
            document.getElementById('usernameError').textContent = '';
        }

        const email = document.getElementById('email').value;
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            document.getElementById('email').classList.add('error');
            document.getElementById('emailError').textContent = '请输入有效的邮箱！';
            isValid = false;
        } else {
            document.getElementById('email').classList.remove('error');
            document.getElementById('emailError').textContent = '';
        }

        const password = document.getElementById('password').value;
        const passwordRegex = /[a-zA-Z0-9]/;
        if (!passwordRegex.test(password)) {
            document.getElementById('password').classList.add('error');
            document.getElementById('passwordError').textContent = '请输入正确的密码格式（aA-zZ，0-9）';
            isValid = false;
        } else {
            document.getElementById('password').classList.remove('error');
            document.getElementById('passwordError').textContent = '';
        }

        if (isValid) {
            // 提交表单
            this.submit();
        }
    });
</script>
</body>
</html>